static void
bcr_rd_init(const QString& fname)
{
- ini = inifile_init(qPrintable(fname), MYNAME);
+ ini = inifile_init(fname, MYNAME);
if (ini->unicode) {
cet_convert_init(CET_CHARSET_UTF8, 1);
}
FILE* xfopen(const char* fname, const char* type, const char* errtxt);
+// Thin wrapper around fopen() that supports Unicode fname on all platforms.
+FILE* ufopen(const QString& fname, const char* mode);
+
// OS-abstracting wrapper for getting Unicode environment variables.
QString ugetenv(const char* env_var);
{
exif_release_apps();
- QString tmpname = QString::fromLocal8Bit(fout->name);
+ QString tmpname = QString(fout->name);
gbfclose(fout);
if (exif_success) {
char* s;
xasprintf(&inipath, "%s/%s", path, "APP/Atlas.ini");
- inifile = inifile_init(inipath, myname);
+ inifile = inifile_init(QString::fromUtf8(inipath), myname);
if (!inifile) {
xfree(inipath);
return NULL;
#include "garmin_device_xml.h"
#include "xmlgeneric.h"
#include <QtCore/QXmlStreamAttributes>
+#include <QtCore/QFile>
#include <cstdio>
#define MYNAME "whatever"
gdx_read(const char* fname)
{
// Test file open-able before gb_open gets a chance to fatal().
- FILE* fin = fopen(fname, "r");
-
- if (fin) {
- fclose(fin);
+ if (QFile(fname).open(QIODevice::ReadOnly)) {
xml_init(fname, gdx_map, NULL);
xml_read();
xml_deinit();
SET_BINARY_MODE(fd);
self->handle.gz = gzdopen(fileno(fd), openmode);
} else {
- self->handle.gz = gzopen(self->name, openmode);
+#if __WIN32__
+ // On Windows, convert UTF-8 to wchar_t[] and use gzopen_w().
+ QString name(self->name);
+ self->handle.gz = gzopen_w((const wchar_t*) name.utf16(), openmode);
+#else
+ // On other platforms, convert to native locale (UTF-8 or other 8-bit).
+ self->handle.gz = gzopen(qPrintable(QString(self->name)), openmode);
+#endif
}
if (self->handle.gz == NULL) {
file->fileungetc = memapi_ungetc;
file->filewrite = memapi_write;
} else {
- /* Be careful to convert back to local8Bit for these c based APIS */
- file->name = xstrdup(qPrintable(filename));
+ file->name = xstrdup(filename);
file->is_pipe = (filename == "-");
/* Do we have a '.gz' extension in the filename ? */
static void
ggv_ovl_rd_init(const QString& fname)
{
- inifile = inifile_init(qPrintable(fname), MYNAME);
+ inifile = inifile_init(fname, MYNAME);
if (inifile->unicode) {
cet_convert_init(CET_CHARSET_UTF8, 1);
}
filename == NULL: try to open global gpsbabel.ini
*/
inifile_t*
-inifile_init(const char* filename, const char* myname)
+inifile_init(const QString& filename, const char* myname)
{
inifile_t* result;
gbfile* fin = NULL;
- if (filename == NULL) {
+ if (filename.isEmpty()) {
fin = open_gpsbabel_inifile();
if (fin == NULL) {
return NULL;
return result;
}
}
+
reads inifile filename into memory
myname represents the calling module
*/
-inifile_t* inifile_init(const char* filename, const char* myname);
+inifile_t* inifile_init(const QString& filename, const char* myname);
void inifile_done(inifile_t* inifile);
int inifile_has_section(const inifile_t* inifile, const char* section);
if (!posnfilenametmp.isEmpty()) {
#if __WIN32__
- MoveFileExA(qPrintable(posnfilenametmp), qPrintable(posnfilename),
+ MoveFileExW((const wchar_t*) posnfilenametmp.utf16(),
+ (const wchar_t*) posnfilename.utf16(),
MOVEFILE_REPLACE_EXISTING);
#endif
QFile::rename(posnfilenametmp, posnfilename);
*/
#include <QtCore/QCoreApplication>
-#include <QtCore/QTextCodec>
-#include <QtCore/QVector>
#include <QtCore/QStack>
#include <QtCore/QString>
#include <QtCore/QTextCodec>
#include <QtCore/QTextStream>
-#include <QtCore/QCoreApplication>
#include "cet.h"
#include "cet_util.h"
#endif
if (gpsbabel_time != 0) { /* within testo ? */
- global_opts.inifile = inifile_init(NULL, MYNAME);
+ global_opts.inifile = inifile_init(QString(), MYNAME);
}
init_vecs();
if (optarg.isEmpty()) { /* from GUI to preserve inconsistent options */
global_opts.inifile = NULL;
} else {
- global_opts.inifile = inifile_init(qPrintable(optarg), MYNAME);
+ global_opts.inifile = inifile_init(optarg, MYNAME);
}
break;
case 'b':
#include "gbfile.h" /* used for csv output */
#include "gbser.h"
#include <QtCore/QDir>
+#include <QtCore/QFile>
#include <cerrno>
#include <cmath>
#include <cstdlib>
//
// It returns a temporary C string - it's totally kludged in to replace
// TEMP_DATA_BIN being string constants.
-static const char* GetTempName(bool backup) {
+static const QString GetTempName(bool backup) {
const char kData[]= "data.bin";
const char kDataBackup[]= "data_old.bin";
-
- QString t = QDir::tempPath();
- t += QDir::separator();
- t += backup ? kDataBackup : kData;
- // If your temp directory isn't representable in Latin1, you're going to
- // have a bad day.
- return t.toLatin1();
+ return QDir::tempPath() + QDir::separator() + (backup ? kDataBackup : kData);
}
#define TEMP_DATA_BIN GetTempName(false)
#define TEMP_DATA_BIN_OLD GetTempName(true)
log_enabled = 0;
init_scan = 0;
- dout = fopen(TEMP_DATA_BIN, "r+b");
+ dout = ufopen(TEMP_DATA_BIN, "r+b");
if (dout == NULL) {
- dout = fopen(TEMP_DATA_BIN, "wb");
+ dout = ufopen(TEMP_DATA_BIN, "wb");
if (dout == NULL) {
- fatal(MYNAME ": Can't create temporary file %s", TEMP_DATA_BIN);
+ fatal(MYNAME ": Can't create temporary file %s",
+ qPrintable(TEMP_DATA_BIN));
return;
}
}
fseek(dout, 0L,SEEK_END);
dsize = ftell(dout);
if (dsize > 1024) {
- dbg(1, "Temp %s file exists. with size %d\n", TEMP_DATA_BIN, dsize);
+ dbg(1, "Temp %s file exists. with size %d\n", qPrintable(TEMP_DATA_BIN),
+ dsize);
dpos = 0;
init_scan = 1;
}
- dbg(1, "Download %s -> %s\n", port, TEMP_DATA_BIN);
+ dbg(1, "Download %s -> %s\n", port, qPrintable(TEMP_DATA_BIN));
// check log status - is logging disabled ?
do_cmd(CMD_LOG_STATUS, "PMTK182,3,7,", &fusage, 2);
dbg(1, "Download %dkB from device\n", (addr_max+1) >> 10);
if (dsize > addr_max) {
- dbg(1, "Temp %s file (%ld) is larger than data size %d. Data erased since last download !\n", TEMP_DATA_BIN, dsize, addr_max);
+ dbg(1, "Temp %s file (%ld) is larger than data size %d. Data erased since last download !\n", qPrintable(TEMP_DATA_BIN), dsize, addr_max);
fclose(dout);
dsize = 0;
init_scan = 0;
- rename(TEMP_DATA_BIN, TEMP_DATA_BIN_OLD);
- dout = fopen(TEMP_DATA_BIN, "wb");
+ QFile::rename(TEMP_DATA_BIN, TEMP_DATA_BIN_OLD);
+ dout = ufopen(TEMP_DATA_BIN, "wb");
if (dout == NULL) {
- fatal(MYNAME ": Can't create temporary file %s", TEMP_DATA_BIN);
+ fatal(MYNAME ": Can't create temporary file %s",
+ qPrintable(TEMP_DATA_BIN));
return;
}
}
fseek(dout, addr, SEEK_SET);
if (fread(line, 1, rcvd_bsize, dout) == rcvd_bsize && memcmp(line, data, rcvd_bsize) == 0) {
dpos = addr;
- dbg(2, "%s same at %d\n", TEMP_DATA_BIN, addr);
+ dbg(2, "%s same at %d\n", qPrintable(TEMP_DATA_BIN), addr);
} else {
- dbg(2, "%s differs at %d\n", TEMP_DATA_BIN, addr);
+ dbg(2, "%s differs at %d\n", qPrintable(TEMP_DATA_BIN), addr);
init_scan = 0;
addr = dpos;
bsize = read_bsize;
dbg(1, "Opening csv output file %s...\n", csv_fname);
// can't use gbfopen here - it will fatal() if file doesn't exist
- if ((cf = fopen(csv_fname, "r")) != NULL) {
+ if ((cf = ufopen(QString::fromUtf8(csv_fname), "r")) != NULL) {
fclose(cf);
warning(MYNAME ": CSV file %s already exist ! Cowardly refusing to overwrite.\n", csv_fname);
return;
static void file_init(const QString& fname)
{
dbg(4, "Opening file %s...\n", qPrintable(fname));
- if (fl = fopen(qPrintable(fname), "rb"), NULL == fl) {
+ if (fl = ufopen(fname, "rb"), NULL == fl) {
fatal(MYNAME ": Can't open file '%s'\n", qPrintable(fname));
}
switch (mtk_device) {
static void
raymarine_rd_init(const QString& fname)
{
- fin = inifile_init(qPrintable(fname), MYNAME);
+ fin = inifile_init(fname, MYNAME);
if (fin->unicode) {
cet_convert_init(CET_CHARSET_UTF8, 1);
}
if (0 == strcmp(fname, "-")) {
return am_writing ? stdout : stdin;
}
- f = fopen(fname, type);
+ f = ufopen(QString::fromUtf8(fname), type);
if (NULL == f) {
fatal("%s cannot open '%s' for %s. Error was '%s'.\n",
errtxt, fname,
return f;
}
+/*
+ * Thin wrapper around fopen() that supports UTF-8 fname on all platforms.
+ */
+FILE*
+ufopen(const QString& fname, const char* mode)
+{
+#if __WIN32__
+ // On Windows standard fopen() doesn't support UTF-8, so we have to convert
+ // to wchar_t* (UTF-16) and use the wide-char version of fopen(), _wfopen().
+ return _wfopen((const wchar_t*) fname.utf16(),
+ (const wchar_t*) QString(mode).utf16());
+#else
+ // On other platforms, convert to native locale (UTF-8 or other 8-bit).
+ return fopen(qPrintable(fname), mode);
+#endif
+}
+
/*
* OS-abstracting wrapper for getting Unicode environment variables.
*/
return QString::fromLocal8Bit(std::getenv(env_var));
#endif
}
+
/*
* Allocate a string using a format list with optional arguments
* Returns -1 on error.
that will be translated to a single \n, making the line len one character shorter than
on linux machines.
*/
- fin = fopen(qPrintable(fname),"rb");
+ fin = ufopen(fname, "rb");
if (!fin) {
fatal("v900: could not open '%s'.\n", qPrintable(fname));
}
static void file_init(const QString& fname)
{
db(1, "Opening file...\n");
- if ((fl = fopen(qPrintable(fname), "rb")) == NULL) {
+ if ((fl = ufopen(fname, "rb")) == NULL) {
fatal(MYNAME ": Can't open file '%s'\n", qPrintable(fname));
}
}